package org.csu.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.demo.common.CommonResponse;
import org.csu.demo.domain.*;
import org.csu.demo.persistence.AddToCartMapper;
import org.csu.demo.persistence.ConfirmOrderMapper;
import org.csu.demo.persistence.ProductMapper;
import org.csu.demo.persistence.ViewItemMapper;
import org.csu.demo.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Repository("recordService")
public class RecordServiceImpl implements RecordService {

    @Autowired
    AddToCartMapper addToCartMapper;
    @Autowired
    ConfirmOrderMapper confirmOrderMapper;
    @Autowired
    ViewItemMapper viewItemMapper;
    @Autowired
    ProductMapper productMapper;

    public CommonResponse<RecordVo> getAllRecords(String username) {
        List<ViewItem> viewItemList = this.getViewItemListByUsername(username);
        List<AddToCart> addToCartList = this.getAddToCartListByUsername(username);
        List<ConfirmOrder> confirmOrderList = this.getConfirmOrderListByUsername(username);
        for(ViewItem viewItem : viewItemList){
            String productId = viewItem.getProductId();
            Product product = productMapper.selectById(productId);
            String productName = product.getName();
            viewItem.setProductName(productName);
        }
        RecordVo recordVo = new RecordVo();
        recordVo.setViewItem(viewItemList);
        recordVo.setAddToCart(addToCartList);
        recordVo.setConfirmOrder(confirmOrderList);
        return CommonResponse.createForSuccess(recordVo);
    }

    public void recordViewItem(String username, String itemId, String productId) {
        int quantity =  this.getViewItemQuantityByUsernameAndItemId(username,itemId);
        if(quantity != 0){
            quantity++;
            this.recordViewItemQuantityByUsernameAndItemId(username,itemId,quantity);
        }else {
            ViewItem viewItem = new ViewItem();
            Date utilDate = new Date();
            viewItem.setQuantity(1);
            viewItem.setUsername(username);
            viewItem.setItemId(itemId);
            viewItem.setProductId(productId);
            viewItem.setDate(utilDate);
            viewItemMapper.insert(viewItem);
        }

    }

    public void recordAddToCart(String username, String itemId, int quantity) {
        AddToCart addToCart = new AddToCart();
        Date utilDate = new Date();
        addToCart.setUsername(username);
        addToCart.setItemId(itemId);
        addToCart.setQuantity(quantity);
        addToCart.setDate(utilDate);
        addToCartMapper.insert(addToCart);
    }

    public void recordConfirmOrder(String username,  int orderId) {
        Date utilDate = new Date();
        ConfirmOrder confirmOrder = new ConfirmOrder();
        confirmOrder.setUsername(username);
        confirmOrder.setOrderId(orderId);
        confirmOrder.setDate(utilDate);
        confirmOrderMapper.insert(confirmOrder);
    }

    public List<ConfirmOrder> getConfirmOrderListByUsername(String username) {
        List<ConfirmOrder> confirmOrderList = new ArrayList<>();
        QueryWrapper<ConfirmOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        confirmOrderList = confirmOrderMapper.selectList(queryWrapper);
        return confirmOrderList;
    }

    public List<AddToCart> getAddToCartListByUsername(String username) {
        List<AddToCart> addToCartList = new ArrayList<>();
        QueryWrapper<AddToCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        addToCartList = addToCartMapper.selectList(queryWrapper);
        return addToCartList;
    }

    public List<ViewItem> getViewItemListByUsername(String username) {
        List<ViewItem> viewItemList = new ArrayList<>();
        QueryWrapper<ViewItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        viewItemList = viewItemMapper.selectList(queryWrapper);
        return viewItemList;
    }
    private int getViewItemQuantityByUsernameAndItemId(String username , String itemId){
        int quantity = 0;
        QueryWrapper<ViewItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("itemId",itemId).eq("username",username).select("quantity");
        ViewItem viewItem = viewItemMapper.selectOne(queryWrapper);
        if(viewItem != null){
            quantity = viewItem.getQuantity();
        }
        return quantity;
    }

    private boolean recordViewItemQuantityByUsernameAndItemId(String username , String itemId , int quantity){
        UpdateWrapper<ViewItem> viewItemQueryWrapper = new UpdateWrapper<>();
        viewItemQueryWrapper.eq("username",username).eq("itemId",itemId).set("quantity",quantity);
        return viewItemMapper.update(viewItemQueryWrapper)>0;
    }

}
